home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++,comp.lang.c
- Subject: Re: Safest way to define a string
- Followup-To: comp.lang.c++,comp.lang.c
- Date: 19 Feb 1996 20:27:23 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4gamfb$4q7@dawn.mmm.com>
- References: <Pine.SOL.3.91.960217124454.26354A-100000@hamlet.uncg.edu>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Bin . Lee (b_lee2@hamlet.uncg.edu) wrote:
-
- > Hi: Folks:
-
- > This is a simplest question, I think many of you know the answer, please
- > help me and send me a email.
-
- > Question is: which is the safest way to define a string ?
-
- > a. some one told me to use the following approach:
- > const int STRLEN = 100;
- > char strTest[STRLEN + 1];
-
- This might be the answer you'd expect from comp.lang.c, but in comp.lang.c++
- my advice would be to use a string class.
-
- > Then always use strTest[STRLEN] =0;
- > This is OK, but if I have to pass a 2D array to function,
- > it will looks like
-
- > int TestFun( char strTest2D[][STRLEN + 1] );
- > This is really awkward.
-
- > If I just want to use array, is it safe to use Array class ? or string
- > class ?
-
- If you want an array of strings, a vector<string> class would work nicely.
-
- > Anyway, my real question is how to prevent memory overflow for array ?
- > can I overload global subscript operation [] and unary operator * ?
-
- If by "memory overflow" you mean trying to store more characters in the
- string than STRLEN (or some other arbitrary fixed maximum) then the answer
- is yes, a good string class will take care of the memory management for
- you. It will also provide you an index operator. (However this is not
- a "global subscript operation,", but specific to the string class.) The
- class may or may not implement unary *, but in any event it will not
- let you "walk the string" with a pointer.
-
- > Thanks in advance, this is important for me, please reply me in mail.
- > I do not want to miss it.
-
- It is proper nettiquette to read replies in the news group unless you
- promise to post a summary to e-mail responses.
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-